/** 
 * Project Name: farm-core 
 * File Name: Test.java 
 * Package Name: com.bugull.farm.core.frame.pool
 * Date: 2017年3月9日下午7:53:58 
 * Copyright (c) 2017, hadlinks All Rights Reserved. 
 * 
 */
package com.bugull.farm.core.frame.pool;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

import com.codahale.metrics.MetricRegistry;

/** 
 * ClassName: Test 
 * Function: 带度量器的自定义线程池使用说明.
 * date: 2017年3月9日 下午7:53:58  
 * 
 * @author songwei (songw@hadlinks.com)
 * @version
 * @since JDK 1.8 
 */
public class Test {
	
	public static final Boolean result = Boolean.TRUE;

	public static void main(String[] args) throws Exception{
		Test test = new Test();
		//创建度量器注册对象metricRegistry
		MetricRegistry metricRegistry = new MetricRegistry();
		//创建自定义线程池OrderedSmartPool，在线程池中注册并定义了一项测量器输出：队列平均长度
		OrderedSmartPool osp = new OrderedSmartPool(5, metricRegistry);
		//自定义一个度量器，指定度量器的信息报告方式，以及输出一份柱状图
		ProjectionMetrics metircs = new ProjectionMetrics(metricRegistry);
		//真正提交任务，并执行使用的线程池是自定义的有序线程池
		Test.TestBuguCallable<Boolean> callable = test.new TestBuguCallable<Boolean>(osp, metircs);
		//每100毫秒往线程池中提交任务，根据日志观察线程池的状态
		while(true){
			osp.submit(callable);
			Thread.sleep(100);
		}
	}


	/**
	 * 自定义一个TestBuguCallable任务
	 * 定义真正的业务逻辑
	 * @param <Boolean>
	 */
	public class TestBuguCallable<Boolean> extends BuguCallable<Boolean>{

		public TestBuguCallable(AbstractSmartPool pool, ProjectionMetrics metrics) {
			super(pool, metrics);
		}

		@Override
		public Boolean call() throws Exception {
		    //计算每个任务的潜在执行时间，即任务被提交到真正被执行时所经历的时间
			getMetrics().latency(Duration.between(Instant.ofEpochMilli(getTime()), Instant.now()));
			System.out.println("ThreadName："+Thread.currentThread().getName()+", "+System.currentTimeMillis());
			return (Boolean) result;
		}
		
	}

	public class TestBuguFutureTask extends BuguFutureTask {

		public TestBuguFutureTask(BuguCallable<Boolean> callable) {
			super(callable);
//			this.bindDoneProxy();
		}

	}
}
